import {Component, Input, OnInit} from '@angular/core';
import {DhlCommonInputComponent} from '../dhlCommonInput';
import {CheckLayoutType} from '../enums';

@Component({
  selector: 'dhl-check',
  templateUrl: './dhl-check.component.html'
})
export class DhlCheckComponent extends DhlCommonInputComponent implements OnInit {

  @Input() spaceWidth = 5;
  @Input() dicCode;
  optionArray = [];

  @Input() set options(value) {
    if (value) {
      for (const item of value) {
        this.optionArray.push({...item});
      }
    } else {
      this.optionArray = [];
    }
  };

  @Input() layout = CheckLayoutType.ROW;


  constructor() {
    super();
  }

  afterInitValue() {
    if (this.value && this.optionArray && this.optionArray.length > 0) {
      this.optionArray.forEach((option) => {
        option.checked = false;
      });

      this.optionArray.forEach((option, x) => {
        this.value.forEach((v, y) => {
          if (option.value == v) {
            option.checked = true;
          }
        });
      });
    }
  }


  modelChange(detail) {
    this.inputValue = detail;
    this.valueChange.emit(detail);
  }

  ngOnInit(): void {
    if (this.dicCode) {
      this.options = this.cacheService.getOptions(this.dicCode);
      console.log(this.options)
    }
    $('[dhl-input-element]', this.inputControl.nativeElement).attr('isArray', 'true');
  }

  reset() {
    if (this.defaultValue == '#EMPTY') {
      this.value = [];
    } else {
      this.value = this.defaultValue;
    }
  }

}







